public class LastPerson {
    public static int findLastPerson(int n) {
        boolean[] isIn = new boolean[n];
        for (int i = 0; i < n; i++) {
            isIn[i] = true;
        }
        int count = 0;
        int index = 0;
        int left = n;
        while (left > 1) {
            if (isIn[index]) {
                count++;
                if (count == 3) {
                    isIn[index] = false;
                    count = 0;
                    left--;
                }
            }
            index++;
            if (index == n) {
                index = 0;
            }
        }
        for (int i = 0; i < n; i++) {
            if (isIn[i]) {
                return i + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int n = 500;
        int result = findLastPerson(n);
        System.out.println("最后剩下的是原来的第 " + result + " 个人");
    }
}